package com.yuezhu.global;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.yuezhu.R;
import com.yuezhu.fragment.AssetFragment;
import com.yuezhu.fragment.MyFragment;
import com.yuezhu.fragment.TentenFragmen;

public class MainActivity extends BaseActivity {
    /**
     * 底部菜单栏viewGroups ;
     */
    private LinearLayout invoicelay, walllay, mylay;
    private TextView invoiceTextView, wallTextView, myTextView;
    private ImageView imgView1, imageView2, imageView3;
    /**
     * 资产
     */
    private TentenFragmen tentenFragment;
    /**
     * 租客
     */
    private AssetFragment assetFragment;

    /**
     * 我的
     */
    private MyFragment myFragment;
    private FragmentManager fragmentManager;
    private FragmentTransaction transaction;
    private View backView;
    private long exitTime = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initFragment();
        setListenters();
    }


    @Override
    protected void initView() {
        super.initView();
        backView = findViewById(R.id.baclin1);
        invoicelay = (LinearLayout) findViewById(R.id.main_bottom_invoice);
        walllay = (LinearLayout) findViewById(R.id.main_bottom_waybill);
        mylay = (LinearLayout) findViewById(R.id.main_bottom_my);
        invoiceTextView = (TextView) findViewById(R.id.main_bottom_invoice1);
        wallTextView = (TextView) findViewById(R.id.main_bottom_waybill1);
        myTextView = (TextView) findViewById(R.id.main_bottom_my1);
        imgView1 = (ImageView) findViewById(R.id.main_bottom_invoice2);
        imageView2 = (ImageView) findViewById(R.id.main_bottom_waybill2);
        imageView3 = (ImageView) findViewById(R.id.main_bottom_my2);
        int screen_width =ApplicationContext.getInstance().getScreen_width();
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) imgView1.getLayoutParams();
        params.width = screen_width / 15;
        params.height = screen_width / 15;
        imageView2.setLayoutParams(params);
        imageView3.setLayoutParams(params);
    }
    private void setListenters() {
        invoicelay.setOnClickListener(this);
        walllay.setOnClickListener(this);
        mylay.setOnClickListener(this);
        invoiceTextView.setSelected(true);
        imgView1.setSelected(true);
        transaction = fragmentManager.beginTransaction();
        transaction.hide(myFragment);
        transaction.hide(assetFragment);
        transaction.show(tentenFragment);
        transaction.commit();
    }

    /**
     * 初始化主界面fragment
     */
    private void initFragment() {
        fragmentManager = getFragmentManager();
        tentenFragment = (TentenFragmen) fragmentManager
                .findFragmentById(R.id.invoice_fragment);
        assetFragment = (AssetFragment) fragmentManager
                .findFragmentById(R.id.waybill_fragment);
        myFragment = (MyFragment) fragmentManager
                .findFragmentById(R.id.my_fragment);
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        transaction = fragmentManager.beginTransaction();
        switch (view.getId()) {
            case R.id.main_bottom_invoice:
                transaction.hide(myFragment);
                transaction.hide(assetFragment);
                transaction.show(tentenFragment);
                transaction.commit();
                invoiceTextView.setSelected(true);
                wallTextView.setSelected(false);
                myTextView.setSelected(false);
                imgView1.setSelected(true);
                imageView2.setSelected(false);
                imageView3.setSelected(false);
                break;
            case R.id.main_bottom_waybill:
                transaction.show(assetFragment);
                transaction.hide(myFragment);
                transaction.hide(tentenFragment);
                transaction.commit();
                invoiceTextView.setSelected(false);
                wallTextView.setSelected(true);
                myTextView.setSelected(false);
                imgView1.setSelected(false);
                imageView2.setSelected(true);
                imageView3.setSelected(false);
                break;
            case R.id.main_bottom_my:
                transaction.hide(assetFragment);
                transaction.hide(tentenFragment);
                transaction.show(myFragment);
                transaction.commit();
                invoiceTextView.setSelected(false);
                wallTextView.setSelected(false);
                myTextView.setSelected(true);
                imgView1.setSelected(false);
                imageView2.setSelected(false);
                imageView3.setSelected(true);
                break;
        }
    }


    //退出程序提示
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(getApplicationContext(), "再按一次退出悦助",
                        Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                finish();
                System.exit(0);
            }
            return true;
        }
        return false;
    }
}
